/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 * 客户管理，代收货款管理
 *  */
import { 
  getClientManagement,
  editClientInfo,
  getCourierInfo,
  getCascaderInfo,
  deleteClientInfo,
  addClientInfo,
} from '@/services/orderManagement';
import {message} from 'antd'

export default {
  namespace: "clientManage",

  state: {
    clientManageList: {
      list: [],
      pagination: null,
    },
    companyList: [],
    courierInfo: {}
  },

  effects: {
    *getClientManageList({ payload, __dva_resolve, __dva_reject }, { call, put }){
      const response = yield call(getClientManagement, payload);

      if (!response) return;
      const { code, data, msg } = response;
      const { list, count, page_size, current_page } = data;

      if(code == 0){
        yield put({
            type: "save",
            payload: {
              clientManageList:{
                list: list || [],
                pagination:{
                  total:count,
                  pageSize: page_size,
                  current: current_page,
                  showQuickJumper:false,
                  showSizeChanger:false,
                }
              }
            },
        });
        __dva_resolve(response);
      }else{
        __dva_reject();
        message.error(msg);
      }
    },
    *addClientInfo({ payload, __dva_resolve, __dva_reject }, { call, put }){
      const response = yield call(addClientInfo, payload);

      if (!response) return;
      const { code, data, msg } = response;

      if(code == 0){
        message.success(msg)
        __dva_resolve(response);
      }else{
        __dva_reject();
        message.error(msg);
      }
    },
    *editClientInfo({ payload, __dva_resolve, __dva_reject }, { call, put }){
      const response = yield call(editClientInfo, payload);

      if (!response) return;
      const { code, data, msg } = response;

      if(code == 0){
        message.success(msg)
        __dva_resolve(response);
      }else{
        __dva_reject();
        message.error(msg);
      }
    },
    *deleteClientInfo({ payload, __dva_resolve, __dva_reject }, { call, put }){
      const response = yield call(deleteClientInfo, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if(code == 0){
        message.success('删除成功')
        __dva_resolve(response);
      }else{
        __dva_reject();
        message.error(msg);
      }
    },
    //获取快递员信息
    *getCourierInfo({ payload, __dva_resolve, __dva_reject }, { call, put }){
      const response = yield call(getCourierInfo, payload);

      if (!response) return;
      const { code, data, msg } = response;

      if(code == 0){
        yield put({
          type: "save",
          payload: {
            courierInfo:data
          },
        });
        __dva_resolve(response);
      }else{
        __dva_reject();
        message.error(msg);
      }
    },
    //获取级联信息
    *getCascaderInfo({ payload, __dva_resolve, __dva_reject }, { call, put }){
      const response = yield call(getCascaderInfo, payload);

      if (!response) return;
      const { code, data, msg } = response;
      let list = []
      for (const key in data) {
        if (data.hasOwnProperty(key)) {
          const element = data[key];
          list.push(element)
        }
      }

      if(code == 0){
        yield put({
          type: "save",
          payload: {
            cascaderInfo:list
          },
        });
        __dva_resolve(list);
      }else{
        __dva_reject();
        message.error(msg);
      }
    },
    
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  }
};